<template>
  <el-dialog v-model="dialogVisible" :title="title" width="1000" destroy-on-close>
    <el-form :data="data" label-position="top">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="导航名称：">
            <el-input v-model="data.NAV_NAME" placeholder="请输入导航名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="导航路径：">
            <el-input v-model="data.NAV_PATH" placeholder="请输入导航路径"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="导航图标：">
            <icon-picker v-model="data.NAV_ICON"></icon-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序号：">
            <el-input type="number" v-model="data.SORT_NUM" placeholder="请输入排序号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="是否启用：">
            <el-switch v-model="data.ENABLED_FLAG" active-text="是" inline-prompt inactive-text="否"></el-switch>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">保存</el-button>
        <el-button type="normal" @click="close">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import request from '../../../../utils/request';
import { ElNotification } from 'element-plus';

const props = defineProps({
  refreshData: {
    type: Function,
    required: true
  }
})

export interface DialogData {
  NAV_NAME: string
  NAV_PATH: string
  NAV_ICON: string
  PARENT_ID: number
  PLATFORM_FLAG: boolean
  ENABLED_FLAG: boolean
  SORT_NUM: number
}
const dialogVisible = ref(false)
const title = ref('')
const type = ref('')
const data = ref<DialogData>({
  NAV_NAME: '',
  NAV_PATH: '',
  NAV_ICON: '',
  PARENT_ID: -1,
  PLATFORM_FLAG: false,
  ENABLED_FLAG: true,
  SORT_NUM: null
})
// 打开弹窗
const show = (inputType: string, inputData: DialogData) => {
  type.value = inputType
  title.value = inputType == 'add' ? '新增导航' : inputType == 'edit' ? '编辑导航' : '导航详情'
  data.value = inputData
  dialogVisible.value = true
}
// 关闭弹窗
const close = () => {
  dialogVisible.value = false
}
// 提交表单
const submit = () => {
  debugger
  const url = type.value == 'add' ? '/nav/add' : '/nav/edit'
  request.post(url, { data: data.value }).then((res: HttpResult) => {
    if (res.code == 200) {
      ElNotification({
        title: '操作成功',
        message: type.value == 'add' ? '新增导航成功' : '编辑导航成功',
        type: 'success'
      })
      close()
      props.refreshData()
    }
  })
}

defineExpose({
  show, close
})
</script>

<style lang="less" scoped></style>